package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.pojo.Setmeal;
import com.xrit.sunnyday.service.SetMealService;
import com.xrit.sunnyday.common.MessageConstant;
import com.xrit.sunnyday.common.PageResult;
import com.xrit.sunnyday.common.QueryPageBean;
import com.xrit.sunnyday.common.Result;
import com.xrit.sunnyday.common.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    //远程调用
    @Reference
    private SetMealService setMealService;
    /*
    查询所有并分页
     */
    @RequestMapping("/findPageAll.do")
    public Result findPageAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageAll = setMealService.findPageAll(queryPageBean);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageAll);
    }
    /*
    根据id删除
     */
    @RequestMapping("/deleteById.do")
    public Result deleteById( Integer id){
        setMealService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
    /*
    图片文件上传
    springmvc中只可用MultipartFile对象接收文件
     */
    @RequestMapping("/upload.do")
    public Result upload(MultipartFile imgFile){
        //1,获取原文件名称
        String filename = imgFile.getOriginalFilename();
        //2-2,使用String对象的substring方法截取文件后缀(从最后一个点开始至末尾结束)
        String filenameLast = filename.substring(filename.lastIndexOf("."));
        //2-3,使用UUID生成随机新文件名称
        UUID uuid = UUID.randomUUID();
        //2-4,拼接(UUID生成的新文件名称+截取的文件后缀)
        String newFilename=uuid+filenameLast;
        try{
            //3,调用七牛云工具类上传该文件(将文件转换为字节类型)
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),newFilename);
            //3-2,创建map将图片名称和域名封装至map中
            Map<Object, Object> map = new HashMap<>();
            map.put("newFilename",newFilename);
            map.put("domain",QiNiuUtils.DOMAIN);
            //3-3,将map返回
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,map);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }
    /*
    添加套餐
     */
    @RequestMapping("/add.do")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        setMealService.add(checkgroupIds,setmeal);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }
    /*
    根据套餐id查询套餐表
     */
    @RequestMapping("/findById.do")
    public Result findById(Integer id){
        Setmeal setMeal=setMealService.findById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("setMeal",setMeal);
        map.put("domain",QiNiuUtils.DOMAIN);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,map);
    }
    /*
    根据套餐id查询套餐检查组中间表
     */
    @RequestMapping("/findSetMealCheckGroupById.do")
    public Result findSetMealCheckGroupById(Integer id){
        List<Integer> checkGroupId=setMealService.findSetMealCheckGroupById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupId);
    }
    /*
    根据id修改
     */
    @RequestMapping("/update.do")
    public Result update(Integer[] ids, @RequestBody Setmeal setmeal ){
        setMealService.update(ids,setmeal);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
